package com.kx.service_exam.service.impl;

import com.kx.service_exam.dao.ExaminationPartMapper;
import com.kx.service_exam.dao.ExaminationPartMapper_My;
import com.kx.service_exam.exception.BusinessException;
import com.kx.service_exam.model.Examination;
import com.kx.service_exam.model.ExaminationPart;
import com.kx.service_exam.service.interfaces.IExaminationPartSV;
import com.kx.service_exam.service.interfaces.IExaminationSV;
import com.kx.service_exam.vo.add.ExaminationAddVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ExaminationPartSVImpl implements IExaminationPartSV {

    @Autowired
    private ExaminationPartMapper examinationPartMapper;

    @Autowired
    private ExaminationPartMapper_My examinationPartMapper_my;

    @Autowired
    private IExaminationSV examinationSV;

    @Override
    public int insertRecord(ExaminationPart record) {

        return examinationPartMapper.insert(record);
    }

    @Override
    public int deleteRecord(String id) {
        if(StringUtils.isEmpty(id)){
            throw new BusinessException("参数[id]不能为空");
        }
        return examinationPartMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateRecord(ExaminationPart record) {
        return examinationPartMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public ExaminationPart selectRecordById(String id) {
        if(StringUtils.isEmpty(id)){
            throw new BusinessException("参数[id]不能为空");
        }
        return examinationPartMapper.selectByPrimaryKey(id);
    }

    @Override
    public int batchInsertExaminationPart(List<ExaminationPart> examinationParts,String examId, String operatorId) {
        if(examinationParts==null || examinationParts.isEmpty()){
            throw new BusinessException("参数[examinationParts]不能为空!");
        }
        if(StringUtils.isEmpty(examId)){
            throw new BusinessException("参数[examId]不能为空!");
        }
        for(ExaminationPart examinationPart : examinationParts){
            Date date = new Date();
            examinationPart.setId(UUID.randomUUID().toString());
            examinationPart.setCreator(operatorId);
            examinationPart.setModifier(operatorId);
            examinationPart.setCreate_time(date);
            examinationPart.setModify_time(date);
            examinationPart.setDelete_flag(0);
        }
        examinationPartMapper_my.batchInsertExaminationPart(examinationParts);
        return 0;
    }

    @Override
    public int addExaminationPart(ExaminationPart examinationPart, String operatorId) {
        if(examinationPart == null ) throw new BusinessException("参数[examinationPart],不能为null");
        if(StringUtils.isEmpty(examinationPart.getExam_id())){
            throw new BusinessException("参数[examinationPart.exam_id],不能为null");
        }
        Examination examination = examinationSV.selectRecordById(examinationPart.getExam_id());
        if(examination == null) {
            throw new BusinessException("参数[examinationPart.exam_id],无效");
        }
        Date date = new Date();
        examinationPart.setId(UUID.randomUUID().toString());
        examinationPart.setCreator(operatorId);
        examinationPart.setModifier(operatorId);
        examinationPart.setCreate_time(date);
        examinationPart.setModify_time(date);
        examinationPart.setDelete_flag(0);
        return examinationPartMapper.insert(examinationPart);
    }
}
